﻿using System;
using System.IO;
using System.Text.RegularExpressions;
using System.Collections;
using System.Configuration;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Jungle.Code.Core;
using Jungle.Code.DAL;
using System.Collections.Generic;
using Jungle.Code.Utils;
using Jungle.Code.BLL;
using Jungle.News;
using Jungle.Users;

namespace Jungle.Admin.Controls
{
	public partial class reNews : BaseUserModule
	{
        protected override void OnInit(EventArgs e)
        {
            if (Node.GetByNodeID(this.section.NodeID).ParentNodeID == 0)
            {
                hp_All.Visible = true;
                DataGrid1.Visible = false;

                NewObject news = new NewObject();
                news.GetAllInList();
                for (int i = 0; i < 5 && i < news.NewsLisr.Count; i++)
                {
                    Jungle.News.WebControls.PreNew pn = new Jungle.News.WebControls.PreNew();
                    pn.NewId = news.NewsLisr[i].NewId;
                    ph_Top5News.Controls.Add(pn);
                }
            }
            else
            {
                SessionKeeper sk = new SessionKeeper();
                sk.SessionId = Session.SessionID.ToString();
                sk.GetData();

                UserManager um = new UserManager();
                um.UserId = sk.UserId;
                um.GetById();

                DataGrid1.PageIndexChanged += new DataGridPageChangedEventHandler(this.Grid_Change);
                this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound);            
            }

            base.OnInit(e);
        }
        protected override void OnLoad(EventArgs e)
        {
            bindData();
            base.OnLoad(e);
        }
        private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Jungle.News.WebControls.PreNew pn = ((Jungle.News.WebControls.PreNew)e.Item.FindControl("PreNew1"));

                DataRowView drv = (DataRowView)e.Item.DataItem;
                int id = (int)drv["id_news"];
                pn.NewId = id;
            }
        }
        void Grid_Change(Object sender, DataGridPageChangedEventArgs e)
        {
            DataGrid1.CurrentPageIndex = e.NewPageIndex;
            bindData();
        }
        private void bindData()
        {
            NewObject n = new NewObject();
            n.GetAllInDS();

            DataGrid1.DataSource = n.NewsDataSet.Tables[0].DefaultView;
            DataGrid1.DataKeyField = "id_news";
            DataGrid1.DataBind();
        }
	}
}